package gro.ecorrdata.edata.controller;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import gro.ecorrdata.edata.annotation.NoneAuth;
import gro.ecorrdata.edata.model.BaseResponse;
import gro.ecorrdata.edata.util.token.TokenHelper;
import gro.ecorrdata.edata.util.token.TokenModel;
import gro.ecorrdata.edata.util.MyUtil;

@RestController
@CrossOrigin
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private TokenHelper tokenHelper;


	@NoneAuth
	@PostMapping
	@RequestMapping("/login")
	public BaseResponse<Object> login(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String captcha = request.getParameter("captcha");
		String uuid = request.getParameter("uuid");
		String sqluuid = "select code from verify where uuid=?"; 
		String code = "";
		try {
			Map<String, Object> cresult = jdbcTemplate.queryForMap(sqluuid, uuid);
			code = (String) cresult.get("code");
			
		} catch (Exception e) {
			return new BaseResponse<>(true, "验证码错误", null);
		}

		String Lcode = code.toLowerCase();
		String Lcaptcha = captcha.toLowerCase();
		// System.out.println(Lcaptcha);
		// System.out.println(Lcode);
		if (!Lcaptcha.equals(Lcode)) {
			return new BaseResponse<>(false, "验证码错误", null);
		}
		String sql = "select id, username, password,role_id from admin_user where `username`= '"+username+"'";
		Integer id = 0;
		try {
			// System.out.println(sql);
			Map<String, Object> result = jdbcTemplate.queryForMap(sql);
			// System.out.println(result);
	
			String sqlPwd = (String) result.get("password");
			// System.out.println(sqlPwd);
			
			if(result == null || !sqlPwd.equals(MyUtil.md5(password))) {
				return new BaseResponse<>(false, "用户名或密码错误1", null);
			}
			id = (Integer) result.get("id");
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseResponse<>(false, "用户名或密码错误2", null);
		}

		String getIp = MyUtil.getIpAddress(request);

		Long ip = 0L;
		if (getIp.equals("::1")) {
			ip = MyUtil.ipToNum("127.0.0.1");
		} else {
			ip = MyUtil.ipToNum(getIp);
		}
		
		TokenModel model = tokenHelper.create(id,ip);
		// // return JsonData.buildSuccess(model);
		return new BaseResponse<>(true, "登陆成功", model);
		// return new BaseResponse<>(true, "查询成功", null);
	}
	
	@NoneAuth
	@RequestMapping("/islogin")
	@GetMapping
	public boolean isLogin(String userId, String token) {
		TokenModel model = new TokenModel(Integer.parseInt(userId), token);

		return tokenHelper.check(model);
	}
	
	@RequestMapping("/logout")
	@PostMapping
	public boolean logout(String token) {
		boolean result=false;
		// Integer userId = (Integer) request.getAttribute(NormalConstant.CURRENT_USER_ID)
	    if(token != null && token.length() != 0) {
			result = tokenHelper.delete(token);
	    }
	    return result;
	}
}
